package vm

import (
	"bytes"
	"go-forth/pkg/forth"
	"io"
)

type stringInput string

func newStringInput(s string) stringInput {
	return stringInput(s)
}

func (s stringInput) Id() int {
	return -1
}

func (s stringInput) Buffer() *bytes.Buffer {
	return bytes.NewBufferString(string(s))
}

func (s stringInput) Refill() bool {
	return false
}

var _ forth.InputSourceSpec = stringInput("")

type genericInput struct {
	io.Reader
	buf bytes.Buffer
}

func newGenericInput(r io.Reader) *genericInput {
	return &genericInput{
		Reader: r,
	}
}

func (in *genericInput) Id() int {
	return 0
}

func (in *genericInput) Buffer() *bytes.Buffer {
	return &in.buf
}

func (in *genericInput) Refill() bool {
	n, err := in.buf.ReadFrom(in)
	if err != nil {
		panic(err)
	}
	return n > 0
}

var _ forth.InputSourceSpec = &genericInput{}
